﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AlfarEngine.Data;

namespace AlfarEngine.Utilities
{
    /// <summary>
    /// Base class of all Database Loading Utilities.
    /// These databases allow to load a resources only once, 
    /// returning the reference to the already loaded resources
    /// </summary>
    /// <typeparam name="T">The referenced Type</typeparam>
    /// <typeparam name="U">The base type the referenced type is designing</typeparam>
    public class LoadingDatabase<T, U> where T:ReferencedData<U>
    {
        static private Dictionary<string, T> sDatabase = new Dictionary<string, T>();

        static public T Load(string pPath)
        {
            if (!sDatabase.ContainsKey(pPath))
            {
                object[] lData = { pPath };

                sDatabase[pPath] = Activator.CreateInstance(typeof(T), lData) as T;
            }

            sDatabase[pPath].IncrementCount();
            return sDatabase[pPath];
        }

        static internal void Unload(string pPath)
        {
            if (sDatabase.ContainsKey(pPath))
            {
                sDatabase.Remove(pPath);
            }
        }
    }
}
